home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / stat / RCS / stat.c,v < prev   
Encoding:
Text File  |  1990-12-04  |  12.5 KB  |  544 lines

  1. head     1.4;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    shirriff:1.4; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.4
  10. date     89.01.29.16.09.07;  author rab;  state Exp;
  11. branches ;
  12. next     1.3;
  13.  
  14. 1.3
  15. date     89.01.29.15.25.40;  author brent;  state Exp;
  16. branches ;
  17. next     1.2;
  18.  
  19. 1.2
  20. date     88.09.29.15.51.30;  author brent;  state Exp;
  21. branches ;
  22. next     1.1;
  23.  
  24. 1.1
  25. date     88.09.29.15.32.16;  author brent;  state Exp;
  26. branches ;
  27. next     ;
  28.  
  29.  
  30. desc
  31. @Simple program to get the attributes of a file
  32. @
  33.  
  34.  
  35. 1.4
  36. log
  37. @Converted printDate to use localtime so that it will automatically
  38. compensate for daylight savings time.
  39. @
  40. text
  41. @/* 
  42.  * stat.c --
  43.  *
  44.  *    Get the complete attributes of a file
  45.  *
  46.  * Copyright 1989 Regents of the University of California
  47.  * Permission to use, copy, modify, and distribute this
  48.  * software and its documentation for any purpose and without
  49.  * fee is hereby granted, provided that the above copyright
  50.  * notice appear in all copies.  The University of California
  51.  * makes no representations about the suitability of this
  52.  * software for any purpose.  It is provided "as is" without
  53.  * express or implied warranty.
  54.  */
  55.  
  56. #ifndef lint
  57. static char rcsid[] = "$Header$";
  58. #endif /* not lint */
  59.  
  60. #include <sprite.h>
  61. #include <fs.h>
  62. #include <stdio.h>
  63. #include <option.h>
  64. #include <sys/types.h>
  65. #include <time.h>
  66.  
  67. /*
  68.  * Default values for parameters
  69.  */
  70. Boolean shortForm = FALSE;
  71. Boolean link = FALSE;
  72. Boolean showDay = FALSE;
  73. Boolean timing = FALSE;
  74. int    numTimes = 1000;
  75.  
  76. Option optionArray[] = {
  77.     OPT_TRUE, "s", (Address)&shortForm, "Output a short form of the attributes",
  78.     OPT_TRUE, "l", (Address)&link, "Get attributes of the link, not the target",
  79.     OPT_TRUE, "d", (Address)&link, "Include day of the week in dates",
  80.     OPT_TRUE, "t", (Address)&timing, "Time N Fs_GetAttributes calls",
  81.     OPT_INT,  "n", (Address)&numTimes, "Number of repititions (with -t)",
  82. };
  83. int numOptions = sizeof(optionArray) / sizeof(Option);
  84.  
  85. int localOffset;
  86. char * PrintDate();
  87.  
  88. /*
  89.  * main --
  90.  *    Collect arguments, then call Stat() for each file.
  91.  */
  92. void
  93. main(argc, argv)
  94.     int argc;
  95.     char **argv;
  96. {
  97.     register ReturnStatus status;
  98.     register int i;
  99.     register char *fileName;
  100.  
  101.     argc = Opt_Parse(argc, argv, optionArray, numOptions, OPT_ALLOW_CLUSTERING);
  102.  
  103.     if (argc < 2) {
  104.     fprintf(stderr, "usage: %s filename [filename ...]\n", 
  105.             argv[0]);
  106.     exit(0);
  107.     }
  108.     Sys_GetTimeOfDay(NULL, &localOffset, NULL);
  109.     localOffset = (localOffset * 60) + 3600;
  110.  
  111.     if (!timing) {
  112.     for (i=1 ; i<argc ; i++) {
  113.         fileName = argv[i];
  114.  
  115.         status = Stat(fileName, link);
  116.         if (status != SUCCESS) {
  117.         fprintf(stderr, "%s: ", fileName);
  118.         Stat_PrintMsg(status, "");
  119.         }
  120.     }
  121.     } else {
  122.     Time before, after;
  123.     Fs_Attributes attrs;
  124.  
  125.     Sys_GetTimeOfDay(&before, NULL, NULL);
  126.     for (i=0 ; i<numTimes ; i++) {
  127.         status = Fs_GetAttributes(argv[1], link, &attrs);
  128.         if (status != SUCCESS) {
  129.         fprintf(stderr, "%s: ", argv[1]);
  130.         Stat_PrintMsg(status, "");
  131.         break;
  132.         }
  133.     }
  134.     Sys_GetTimeOfDay(&after, NULL, NULL);
  135.     if (i > 0) {
  136.         Time_Subtract(after, before, &after);
  137.         printf("%d Fs_GetAttributes of %s at %dus each\n", i, argv[1],
  138.         (after.seconds * 1000000 + after.microseconds) / i);
  139.     }
  140.     }
  141.     exit(status);
  142. }
  143.  
  144. /*
  145.  * Stat --
  146.  *    Get the attributes of a file and print them.
  147.  *
  148.  * Results:
  149.  *    The status code from Fs_GetAttributes
  150.  *
  151.  * Side Effects:
  152.  *    None.
  153.  */
  154. ReturnStatus
  155. Stat(fileName, link)
  156.     char *fileName;    /* File to get the attributes of */
  157.     Boolean link;    /* If TRUE the attributes of a link file are printed,
  158.              * otherwise the attributes of the target of the link 
  159.              * are printed */
  160. {
  161.     ReturnStatus status;
  162.     Fs_Attributes attrs;
  163.  
  164.     status = Fs_GetAttributes(fileName, link, &attrs);
  165.     if (status != SUCCESS) {
  166.     return(status);
  167.     }
  168.  
  169.     PrintFileType(attrs.type);
  170.     PrintFilePermissions(attrs.permissions);
  171.     printf("%2d  ID=(%d,%d) ", attrs.numLinks, attrs.uid, attrs.gid);
  172.     if (shortForm) {
  173.     /*
  174.      * Print a 1-line summary of the attributes.
  175.      */
  176.     if (attrs.type == FS_DEVICE) {
  177.         printf("%5d%5d ", attrs.devType, attrs.devUnit);
  178.     } else {
  179.         printf("%10d ", attrs.size);
  180.     }
  181.     printf("%s %s\n", PrintDate(attrs.dataModifyTime), fileName);
  182.     } else {
  183.     /*
  184.      * Print the complete attributes.
  185.      */
  186.     printf("%7d bytes  %s\n", attrs.size, fileName);
  187.  
  188.     printf("%7s %7s %10s", "Server", "Domain", "File #");
  189.     if (attrs.type == FS_DEVICE) {
  190.         printf("  Device: %7s %7s %7s", "Server", "Type", "Unit");
  191.     }
  192.     printf("\n");
  193.  
  194.     printf("%7d %7d %10d", 
  195.             attrs.serverID, attrs.domain, attrs.fileNumber);
  196.     if (attrs.type == FS_DEVICE) {
  197.         printf("          %7d %7d %7d", 
  198.         attrs.devServerID, attrs.devType, attrs.devUnit);
  199.     }
  200.     printf("\n");
  201.  
  202.     printf("Version %d    UserType 0x%x\n",
  203.         attrs.version, attrs.userType);
  204.  
  205.     printf("Created:         %s\n", PrintDate(attrs.createTime));
  206.     printf("Data modified:   %s\n", PrintDate(attrs.dataModifyTime));
  207.     printf("Descr. modified: %s\n", PrintDate(attrs.descModifyTime));
  208.     printf("Last accessed:   %s\n", PrintDate(attrs.accessTime));
  209.     }
  210.     return(SUCCESS);
  211. }
  212.  
  213. /*
  214.  * PrintFileType --
  215.  *    Print a character to represent a file's type.
  216.  */
  217. PrintFileType(type)
  218.     int type;
  219. {
  220.     char c;
  221.     switch(type) {
  222.     case FS_FILE:
  223.         c = '-';
  224.         break;
  225.     case FS_DIRECTORY:
  226.         c = 'd';
  227.         break;
  228.     case FS_SYMBOLIC_LINK:
  229.         c = 'l';
  230.         break;
  231.     case FS_REMOTE_LINK:
  232.         c = 'r';
  233.         break;
  234.     case FS_DEVICE:
  235.     case FS_REMOTE_DEVICE:
  236.         c = 'D';
  237.         break;
  238.     case FS_NAMED_PIPE:
  239.         c = 'p';
  240.         break;
  241.     case FS_PSEUDO_DEV:
  242.         c = 'x';
  243.         break;
  244.     default:
  245.         printf("(%d)", type);
  246.         return;
  247.     }
  248.     printf("%c", c);
  249. }
  250. /*
  251.  * PrintFilePermissions --
  252.  *    Print out characters to represent the permission bits of a file.
  253.  */
  254. PrintFilePermissions(permissions)
  255.     int permissions;
  256. {
  257.     char c;
  258.     register int i;
  259.     register int bits;
  260.  
  261.     if (permissions & FS_SET_UID) {
  262.     printf("u");
  263.     } else if (permissions & FS_SET_GID) {
  264.     printf("g");
  265.     } else {
  266.     printf("-");
  267.     }
  268.     for (i=0 ; i<3 ; i++) {
  269.     bits = (permissions >> ((2-i)*3)) & 0x7;
  270.     if (bits & FS_WORLD_READ) { c = 'r'; } else { c = '-'; }
  271.     printf("%c", c);
  272.     if (bits & FS_WORLD_WRITE) { c = 'w'; } else { c = '-'; }
  273.     printf("%c", c);
  274.     if (bits & FS_WORLD_EXEC) { c = 'x'; } else { c = '-'; }
  275.     printf("%c", c);
  276.     }
  277. }
  278. /*
  279.  * PrintDate --
  280.  *    Print a date in ascii format
  281.  */
  282.  
  283. char *
  284. PrintDate(time)
  285.     Time time;
  286. {
  287.     time_t t;
  288.     char *string;
  289.  
  290.     t = time.seconds;
  291.     string = asctime(localtime(&t));        
  292.     string[strlen(string) - 1] = '\0';      /* remove trailing '\n' */
  293.     if (showDay) {
  294.     return(string);
  295.     } else {
  296.     return(&string[4]);
  297.     }
  298. }
  299. @
  300.  
  301.  
  302. 1.3
  303. log
  304. @Nuked help feature, which was out-of-date
  305. @
  306. text
  307. @d1 13
  308. a13 2
  309. /*
  310.  * stat - Get the complete attributes of a file
  311. a14 4
  312. #include "sprite.h"
  313. #include "fs.h"
  314. #include "stdio.h"
  315. #include "option.h"
  316. d16 11
  317. d52 1
  318. d55 1
  319. a55 1
  320.     char *argv[];
  321. d74 1
  322. a74 1
  323.     
  324. d247 2
  325. a248 1
  326.     static char string[50];
  327. d250 3
  328. a252 1
  329.     Time_ToAscii(time.seconds + localOffset, FALSE, string);
  330. @
  331.  
  332.  
  333. 1.2
  334. log
  335. @Ported to standard C library
  336. @
  337. text
  338. @a11 1
  339. Boolean help = FALSE;
  340. a18 1
  341.     OPT_TRUE, "h", (Address)&help, "Print explaination of the fields",
  342. a43 31
  343.     if (help) {
  344.     if (shortForm) {
  345.         printf("The short form of %s output is:\n", argv[0]);
  346.         printf("Tfppppppppp owner size modifyDate fileName\n");
  347.         printf(" T is file type: (-, file)\n");
  348.         printf("                 (d, directory)\n");
  349.         printf("                 (l, symbolic link)\n");
  350.         printf("                 (r, remote link)\n");
  351.         printf("                 (D, device)\n");
  352.         printf("                 (p, pipe)\n");
  353.         printf(" f is S for SetUid, - otherwise\n");
  354.         printf(" ppppppppp are the 9 permission bits\n");
  355.     } else {
  356.         printf("The long form of %s output is:\n", argv[0]);
  357.         printf("fileName\tIn-core flags\n");
  358.         printf("<fileServer, fileDomain, fileNumber>, creation date\n");
  359.         printf("<devServer, devType, devUnit>, dataModify date\n");
  360.         printf("owner, group, descModify date\n");
  361.         printf("Tfppppppppp, numLinks, size, accessDate\n");
  362.         printf(" T is file type: (-, file)\n");
  363.         printf("                 (d, directory)\n");
  364.         printf("                 (l, symbolic link)\n");
  365.         printf("                 (r, remote link)\n");
  366.         printf("                 (D, device)\n");
  367.         printf("                 (p, pipe)\n");
  368.         printf("                 (x, pseudo-device)\n");
  369.         printf(" f is u for SetUid, g for SetGid, - otherwise\n");
  370.         printf(" ppppppppp are the 9 permission bits\n");
  371.     }
  372.     exit(SUCCESS);
  373.     }
  374. @
  375.  
  376.  
  377. 1.1
  378. log
  379. @Initial revision
  380. @
  381. text
  382. @a4 2
  383. #include "kernel/fs.h"
  384. #include "kernel/fsInt.h"
  385. d6 1
  386. a6 3
  387. #include "io.h"
  388. #include "rpc.h"
  389. #include "proc.h"
  390. d20 6
  391. a25 6
  392.     OPT_TRUE, 'h', (Address)&help, "Print explaination of the fields",
  393.     OPT_TRUE, 's', (Address)&shortForm, "Output a short form of the attributes",
  394.     OPT_TRUE, 'l', (Address)&link, "Get attributes of the link, not the target",
  395.     OPT_TRUE, 'd', (Address)&link, "Include day of the week in dates",
  396.     OPT_TRUE, 't', (Address)&timing, "Time N Fs_GetAttributes calls",
  397.     OPT_INT,  'n', (Address)&numTimes, "Number of repititions (with -t)",
  398. d44 1
  399. a44 1
  400.     Opt_Parse(&argc, argv, numOptions, optionArray);
  401. d48 10
  402. a57 10
  403.         Io_Print("The short form of %s output is:\n", argv[0]);
  404.         Io_Print("Tfppppppppp owner size modifyDate fileName\n");
  405.         Io_Print(" T is file type: (-, file)\n");
  406.         Io_Print("                 (d, directory)\n");
  407.         Io_Print("                 (l, symbolic link)\n");
  408.         Io_Print("                 (r, remote link)\n");
  409.         Io_Print("                 (D, device)\n");
  410.         Io_Print("                 (p, pipe)\n");
  411.         Io_Print(" f is S for SetUid, - otherwise\n");
  412.         Io_Print(" ppppppppp are the 9 permission bits\n");
  413. d59 15
  414. a73 15
  415.         Io_Print("The long form of %s output is:\n", argv[0]);
  416.         Io_Print("fileName\tIn-core flags\n");
  417.         Io_Print("<fileServer, fileDomain, fileNumber>, creation date\n");
  418.         Io_Print("<devServer, devType, devUnit>, dataModify date\n");
  419.         Io_Print("owner, group, descModify date\n");
  420.         Io_Print("Tfppppppppp, numLinks, size, accessDate\n");
  421.         Io_Print(" T is file type: (-, file)\n");
  422.         Io_Print("                 (d, directory)\n");
  423.         Io_Print("                 (l, symbolic link)\n");
  424.         Io_Print("                 (r, remote link)\n");
  425.         Io_Print("                 (D, device)\n");
  426.         Io_Print("                 (p, pipe)\n");
  427.         Io_Print("                 (x, pseudo-device)\n");
  428.         Io_Print(" f is u for SetUid, g for SetGid, - otherwise\n");
  429.         Io_Print(" ppppppppp are the 9 permission bits\n");
  430. d75 1
  431. a75 1
  432.     Proc_Exit(SUCCESS);
  433. d78 1
  434. a78 1
  435.     Io_PrintStream(io_StdErr, "usage: %s filename [filename ...]\n", 
  436. d80 1
  437. a80 1
  438.     Proc_Exit(0);
  439. d91 1
  440. a91 1
  441.         Io_PrintStream(io_StdErr, "%s: ", fileName);
  442. d103 1
  443. a103 1
  444.         Io_PrintStream(io_StdErr, "%s: ", argv[1]);
  445. d111 1
  446. a111 1
  447.         Io_Print("%d Fs_GetAttributes of %s at %dus each\n", i, argv[1],
  448. d115 1
  449. a115 1
  450.     Proc_Exit(status);
  451. d145 1
  452. a145 1
  453.     Io_Print("%2d  ID=(%d,%d) ", attrs.numLinks, attrs.uid, attrs.gid);
  454. d151 1
  455. a151 1
  456.         Io_Print("%5d%5d ", attrs.devType, attrs.devUnit);
  457. d153 1
  458. a153 1
  459.         Io_Print("%10d ", attrs.size);
  460. d155 1
  461. a155 1
  462.     Io_Print("%s %s\n", PrintDate(attrs.dataModifyTime), fileName);
  463. d160 1
  464. a160 1
  465.     Io_Print("%7d bytes  %s\n", attrs.size, fileName);
  466. d162 1
  467. a162 1
  468.     Io_Print("%7s %7s %10s", "Server", "Domain", "File #");
  469. d164 1
  470. a164 1
  471.         Io_Print("  Device: %7s %7s %7s", "Server", "Type", "Unit");
  472. d166 1
  473. a166 1
  474.     Io_Print("\n");
  475. d168 1
  476. a168 1
  477.     Io_Print("%7d %7d %10d", 
  478. d171 1
  479. a171 1
  480.         Io_Print("          %7d %7d %7d", 
  481. d174 1
  482. a174 1
  483.     Io_Print("\n");
  484. d176 1
  485. a176 1
  486.     Io_Print("Version %d    UserType 0x%x\n",
  487. d179 4
  488. a182 4
  489.     Io_Print("Created:         %s\n", PrintDate(attrs.createTime));
  490.     Io_Print("Data modified:   %s\n", PrintDate(attrs.dataModifyTime));
  491.     Io_Print("Descr. modified: %s\n", PrintDate(attrs.descModifyTime));
  492.     Io_Print("Last accessed:   %s\n", PrintDate(attrs.accessTime));
  493. d219 1
  494. a219 1
  495.         Io_Print("(%d)", type);
  496. d222 1
  497. a222 1
  498.     Io_Print("%c", c);
  499. a224 25
  500.  * PrintFileFlags --
  501.  *    Print out characters to represent the flag bits of a file.
  502.  */
  503. PrintFileFlags(flags)
  504.     int flags;
  505. {
  506.     Io_Print("Flags:    ");
  507.     if (flags & FS_CACHEABLE) {
  508.     Io_Print("Cacheable  ");
  509.     }
  510.     if (flags & FS_WRITE_THRU) {
  511.     Io_Print("Write-thru  ");
  512.     }
  513.     if (flags & FS_FILE_DELETED) {
  514.     Io_Print("Deleted  ");
  515.     }
  516.     if (flags & FS_SHARED_LOCK) {
  517.     Io_Print("Shared lock  ");
  518.     }
  519.     if (flags & FS_EXCLUSIVE_LOCK) {
  520.     Io_Print("Exclusive lock  ");
  521.     }
  522.     Io_Print("\n");
  523. }
  524. /*
  525. d236 1
  526. a236 1
  527.     Io_Print("u");
  528. d238 1
  529. a238 1
  530.     Io_Print("g");
  531. d240 1
  532. a240 1
  533.     Io_Print("-");
  534. d245 1
  535. a245 1
  536.     Io_Print("%c", c);
  537. d247 1
  538. a247 1
  539.     Io_Print("%c", c);
  540. d249 1
  541. a249 1
  542.     Io_Print("%c", c);
  543. @
  544.